package com.winai.launcher.override;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.winai.launcher.R;

/**
 * @author kangshao
 * @date 3/25/21
 * @desc 我的设备 在线-离线
 */
public class PointView extends View {
    private Paint paint, textPaint;
    private boolean isOnline = false;
    private RectF rectF;

    public PointView(Context context) {
        super(context);
        init(context, null);
    }

    public PointView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public PointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            //从xml的属性中获取到字体颜色与string
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PointView);
            isOnline = ta.getBoolean(R.styleable.PointView_isOnline, false);
            ta.recycle();
        }

        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(10);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);

        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setAntiAlias(true);
        textPaint.setStrokeWidth(18);
        textPaint.setStrokeCap(Paint.Cap.ROUND);
        textPaint.setStrokeJoin(Paint.Join.ROUND);
    }

    public void setOnline(boolean online) {
        isOnline = online;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF = new RectF(5, 2.5f, 15, 12.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String text;
        if (isOnline) {
            text = "在线";
            paint.setColor(Color.GREEN);
        } else {
            text = "离线";
            paint.setColor(Color.RED);
        }
        canvas.drawRoundRect(rectF, 15, 15, paint);

        canvas.drawText(text, 20, 12.5f, textPaint);
    }

    /**
     * 对View进行测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }


    /**
     * 计算宽度
     *
     * @param measureSpec
     * @return
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //MeasureSpec共3种测量模式
        //EXACZTLY  精确值模式，layout_width或者layout_height属性设置为具体数值，或指定为match_parent时
        //AT_MOST 最大值模式，layout_width或者layout_height属性设置为wrap_content时，此时空间尺寸只要不超过父控件允许的最大尺寸即可
        //UNSPECIFIED 不限制View大小
        if (specMode == MeasureSpec.EXACTLY) {  //如果为EXACZTLY模式，直接使用指定specSize
            result = specSize;
        } else {
            result = 50;   //否则的话，specSize设置为200
            if (specMode == MeasureSpec.AT_MOST) {  //如果模式为AT_MOST模式，则取出我们指定的大小与specSize中小的那个座位最后的测量值
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * 计算高度（与计算宽度方法基本相同）
     *
     * @param measureSpec
     * @return
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 15;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

}
